<?php
/*
 * Copyright (c) 2022 西部盒子.
 * Author:  @luge
 * Update: 2022/5/20 上午9:45
 */


add_shortcode('wboxupdatelog', function ($args) {
    $term_id = $args['cat'] ?? 0;
    $args = array(
        'post_type' => 'wb_log',
        'tax_query' => array(
                array(
                    'taxonomy' => 'wb_log_cat',
                    'field' => 'id',
                    'terms' => array(absint($term_id)),
                    'operator' => 'IN'
                ),
            ),
        'showposts' => 9999,
        'orderby' => 'post_date',
        'order' => get_option('wbox_update_log_order', 'DESC')
    );
    $query = new WP_Query($args);
    if(!$query->have_posts()){
        wp_reset_postdata();
        return '';
    }

    $css = get_option('wb_time_line_css', '');
    if (get_option('wbox_update_log_showstyle', 1) > 1) {
        $html = WBUL_do_shortcode_list($query, $css);
        wp_reset_postdata();
        return $html;
    }
    $html = WBUL_do_shortcode_timeline($query, $css);
    wp_reset_postdata();
    return $html;
});

//时间轴样式
function WBUL_do_shortcode_timeline($query, $css): string {
    $posts = array();
    $dateY = '';
    while ($query->have_posts()){
        $query->the_post();
        $date = get_the_time('Y-m-d');

        //首条数据是全日期格式
        if($dateY == ''){
            $dateY = $date;
        }elseif(date('Y',strtotime($dateY .' 18:00')) != date('Y',strtotime($date .' 18:00'))){
            $dateY = date('Y',strtotime($date .' 18:00'));
        }

        $posts[$dateY][] = array(
            'time' => $date,
            'ver' => get_the_title(),
            'content' => wpautop(get_the_content()),
        );
    }
    ob_start();
?>
<style>
    .WBUL-body{
        --wbul-font-color: #000000;
        --wbul-primary-color: #49c9e0;
        font-size: 14px;
    }
    .WBUL-body *{
        color:var(--wbul-font-color);
        margin: 0;
        padding: 0;
        border: unset;
        box-sizing: border-box;
        line-height: 1;
    }
    .WBUL-list{
        list-style: none;
        padding-left: 150px!important;
        margin:0 0 30px 0!important;
    }
    .WBUL-list-h2{
        display: inline-block;
        margin: 10px 0;
        padding-left: 0;
        font-weight: 600;
        font-size: 22px;
        line-height: 1em;
        -webkit-transform: translate(-50%,0);
        -moz-transform: translate(-50%,0);
        -ms-transform: translate(-50%,0);
        -o-transform: translate(-50%,0);
        transform: translate(-50%,0);
    }
    .WBUL-list-li{
        position: relative!important;
        padding: 25px 0 20px 20px!important;
        margin: 0!important;
        list-style: none!important;
    }
    .WBUL-list-li:before {
        content: '';
        box-sizing: border-box;
        width: 1px;
        height: 100%;
        background: var(--wbul-primary-color);
        position: absolute;
        left: 0;
        top: 0;
    }
    .WBUL-list-h3 {
        margin: 0!important;
        position: absolute;
        font-size: 20px;
        left: -25px;
        top: 25px;
        -webkit-transform: translate(-100%,0);
        -moz-transform: translate(-100%,0);
        -ms-transform: translate(-100%,0);
        -o-transform: translate(-100%,0);
        transform: translate(-100%,0);
    }
    .WBUL-list-circle {
        position: absolute;
        border-radius: 50%;
        background-color: #fff;
        font-weight: 700;
        width: 20px;
        height: 20px;
        line-height: 8px;
        padding: 5px;
        text-align: center;
        border: 2px solid var(--wbul-primary-color);
        color: var(--wbul-primary-color);
        left: -10px;
        top: 26px;
    }
    .WBUL-list-h4{
        margin: 0!important;
        font-size: 20px;
        font-weight: 700;
    }
    .WBUL-list-card{
        position: relative;
        display: -ms-flexbox;
        display: flex;
        -ms-flex-direction: column;
        flex-direction: column;
        min-width: 0;
        word-wrap: break-word;
        background-clip: border-box;
        border: 1px solid rgba(0,0,0,.125);
        border-radius: 5px;
        background-color: #f8f9fa;
        margin-top: 10px;
        padding: 20px;
        overflow-y: auto;
    }
    .WBUL-list-card::-webkit-scrollbar {
        width: 5px;
    }
    .WBUL-list-card::-webkit-scrollbar-thumb {
         background-color: var(--wbul-primary-color);
     }
    .WBUL-list-card::-webkit-scrollbar-track {
        background-color: #e5e5e5;
    }
    .WBUL-list-content{
        width: 100%;
        max-height: 300px;
    }
    .WBUL-list-content ul,.WBUL-list-content ol{
        padding-left: 20px;
    }
    .WBUL-list-content *{
        color: #6c757d;
        margin: 0!important;
        line-height: 1.5!important;
    }
    @media (max-width: 767px){
        .WBUL-list {
            list-style: none;
            padding-left: 0!important;
        }
        .WBUL-list-h2,.WBUL-list-h3 {
            -webkit-transform: unset;
            -moz-transform: unset;
            -ms-transform: unset;
            -o-transform: unset;
            transform: unset;
        }
        .WBUL-list-h3{
            position: static;
        }
    }
    <?php echo $css; ?>
</style>
<div class="WBUL-body">
    <ul class="WBUL-list">
        <?php foreach ($posts as $year => $datas): ?>
        <span class="WBUL-list-h2"><?php echo $year ?></span>
        <?php foreach ($datas as $data): ?>
        <li class="WBUL-list-li">
            <span class="WBUL-list-h3"><?php echo $data['time'] ?></span>
            <span class="WBUL-list-circle">·</span>
            <dl class="WBUL-list-dl">
                <dt><span class="WBUL-list-h4"><?php echo $data['ver'] ?> 更新记录</span></dt>
                <dd class="WBUL-list-card">
                    <div class="WBUL-list-content entry-content">
                        <?php echo $data['content'] ?>
                    </div>
                </dd>
            </dl>
        </li>
        <?php endforeach;endforeach; ?>
    </ul>
</div>
<?php
    return wbox_compress_html(ob_get_clean());
}

function WBUL_do_shortcode_list($query, $css): string {
ob_start();
?>
    <style>
        .WBUL-body{
            font-size: 14px;
        }
        .WBUL-body *{
            margin: 0;
            padding: 0;
            border: unset;
            box-sizing: border-box;
        }
        .WBUL-list{
            list-style: none;
        }
        h3.WBUL-list-h3,h4.WBUL-list-h4 {
            display: inline-block;
            color: #333;
            font-size: 18px;
            line-height: 2em;
            font-weight: 700;
        }
        h4.WBUL-list-h4 {
            padding-left: 20px;
            font-size: 16px;
            color: #666;
            font-weight: 200;
        }
        .WBUL-list-content{
            padding-left: 20px;
        }
        .WBUL-list-content *{
            color: #6c757d;
        }
        <?php echo $css; ?>
    </style>
    <div class="WBUL-body">
        <ul class="WBUL-list">
        <?php while ($query->have_posts()): $query->the_post(); ?>
            <li class="WBUL-list-li">
                <h3 class="WBUL-list-h3"><?php the_title(); ?> 更新记录</h3>
                <h4 class="WBUL-list-h4"><?php the_time("Y-m-d"); ?></h4>
                <div class="WBUL-list-content entry-content">
                    <?php echo wpautop(get_the_content()); ?>
                </div>
            </li>
        <?php endwhile; ?>
        </ul>
    </div>
<?php
    return wbox_compress_html(ob_get_clean());
}

if(!function_exists('wbox_compress_html')){
    function wbox_compress_html($string) {
        $string  = str_replace("\r\n", '', $string); //清除换行符
        $string  = str_replace("\n", '', $string); //清除换行符
        $string  = str_replace("\t", '', $string); //清除制表符
        $pattern = array(
            "/> *([^ ]*) *</", //去掉注释标记
            "/[\s]+/",
            "/<!--[^!]*-->/",
            "/\" /",
            "/ \"/",
            "'/\*[^*]*\*/'",
        );
        $replace = array(
            ">\\1<",
            " ",
            "",
            "\"",
            "\"",
            "",
        );
        return preg_replace($pattern, $replace, $string);
    }
}